/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyThread;

import MyLib.Comvar;
import XML.SAXFindChunk;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.ByteBuffer;

/**
 *
 * @author QuangHau
 */
public class responFindChunk extends Thread {

    private DatagramPacket packet;

    public responFindChunk(DatagramPacket rcvpkt) {
        packet = rcvpkt;
        
    }

    public void run() {
        String stRcv; 
        //get header
        byte[] header = new byte[14];
        System.arraycopy(packet.getData(), 0, header, 0, 14);

        //get lenght
        byte[] arr = {header[2], header[3]};
        ByteBuffer wrapped = ByteBuffer.wrap(arr); // big-endian by default
        short lenght = wrapped.getShort();

        //get data   
        byte[] data = new byte[lenght];
        System.arraycopy(packet.getData(), 14, data, 0, lenght);
        stRcv = new String(data, 0, lenght);
        Comvar.writeLog(3, responFindChunk.class.getName(), "RECEIVE " + packet.getAddress().getHostAddress() + 
                                        " [Find chunk " + stRcv + "]");
        System.out.println("Nha yeu cau tim chunk " + stRcv);
        //Tìm Chunk
        String[] arrStr = stRcv.split(";");
        if (arrStr.length == 2 && SAXFindChunk.findChunk(arrStr[0], arrStr[1])) {
            //Gui lai cho client
            try {

                //khoi tao socket
                DatagramSocket socket = new DatagramSocket();
                int port = 35617;
                //Gan dia chi IP va port vao InetAddress
                //InetAddress ipServer= InetAddress.getByName("localhost");
                InetAddress ipServer = packet.getAddress();
                
                /*----Tạo data----*/
                String stSend = stRcv;
                data = stSend.getBytes();

                /*----Tạo header-----*/
                header = new byte[14];
                //type: tra loi tìm kiếm trunk
                header[0] = 11;
                //lenght
                short num = (short) (data.length);
                ByteBuffer dbuf = ByteBuffer.allocate(2);
                dbuf.putShort(num);
                byte[] len = dbuf.array();
                System.arraycopy(len, 0, header, 2, 2);

                /*----Tạo gói tin-----*/
                byte[] sendData = new byte[1024];
                //gắn header
                System.arraycopy(header, 0, sendData, 0, 14);
                //gắn data
                System.arraycopy(data, 0, sendData, 14, data.length);

                /*----gửi-----*/
                //Khoi tao DatagramPacket
                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ipServer, port);
                //Send DatagramPacket

                socket.send(sendPacket);
                Comvar.writeLog(3, responFindChunk.class.getName(), "SEND " + packet.getAddress().getHostAddress() + 
                                        " [Respont find chunk " + stRcv + "]");
                System.out.println("gui lai cho client" + packet.getAddress() + " chunk " + stSend);
                //dong DatagramSocket
                socket.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
